<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */
require_once ( LIB_DIR.'model/ReviewDAO.php');
require_once ( LIB_DIR.'search/reviewsearch.php');
require_once ( LIB_DIR.'classes/Pager.php' );

//parse url parameters
$reviews = array();
$reviewCount = 0;
$pageVars = array();

$pid= intval( edp_db_input( $_GET['pid'] ) );
$pageVars['pid'] = $pid;

//check whether it is search request
$keyword = "";
if( isset ( $_GET['q']) ) {
	$keyword = htmlspecialchars( $_GET['q'] );
	$pageVars['keyword'] = $keyword;
}

$page = 1;
if( isset( $_GET['p'] ) ) {
	$page = intval( $_GET['p'] );
}

$reviewdao = GetReviewDAO();
if( isset ( $_GET['q']) ) {

    $reviewsearch = new ReviewSearch();
    $searchType = FILTER_QUERY;

    $sortby = 'review_date-desc';
    if( isset( $_GET['sortby'] ) )
        $sortby = htmlspecialchars( edp_db_input( $_GET['sortby'] ) );

    //determine search type
    $isDesc = false;
    if( !empty( $sortby ) ) {
        $pageVars['sortby'] = $sortby;

        //parse sort
        //price0 is sort by price asc and price1 is sortby price desc
        $sortbyArray = explode( "-", $sortby );
        if( count($sortbyArray) == 2 ) {
            $sortby = $sortbyArray[0];
            $isDesc = $sortbyArray[1] == "asc"? false : true;
        }
        else {
            $sortby = "";
        }

        if( $sortby != "" ) {
            if( $searchType == FILTER_QUERY ) {
                $searchType = FILTER_SORT_QUERY;
            }
            else {
                $searchType = SORT_QUERY;
            }
        }
    }

    //default product review display is a filter search
    $filterArray['product_id_list'] = array( intval($pid) );

    $result = array();
    switch( $searchType ) {
        case FILTER_QUERY:
            $result = $reviewsearch->MultiFilterQuery( $keyword, $filterArray, $page );
            break;
        case FILTER_SORT_QUERY:
            $result = $reviewsearch->MultiFilterAndSortQuery( $keyword, $filterArray, $sortby, $page, $isDesc );
            break;
    }

    if( $result[ 'total_found'] > 0 ){
        $reviewCount = $result[ 'total_found'];
        //database query detail information
        $reviews = $reviewdao->GetByIdList( $result[ 'matchIds'] );

        $highLighReviews = array();
        foreach( $reviews as $row ) {
            $row['tags_plain'] = $row['tags'];
        //assemble field to highlight : tag, content
            $arrToHighlight = array
                (
                //hard code here, need to be modified later
                'content' => sconv( EdpSubStr($row['content'], 0, 200) ),
                'tag' => sconv($row['tag'])
            );
            // merge database and high light result array
            $row = array_merge( $row, $reviewsearch->BuildHighLight( $keyword, $arrToHighlight ) );
            if( !empty($row['tag']) ) {//replace the tag highlight
                $row['tags'] = explode( ",", $row['tag']);
                $row['tags'] = array();
                //replace the tag highlight
                foreach( $row['tags_plain'] as $strtag ){
                    if( !empty($keyword) && strpos( $strtag, $keyword ) !== false){
                        $row['tags'][] = str_replace( $keyword, "<span class=\"hilight\">".$keyword."</span>", $strtag );	
                    }
                    else{
                        $row['tags'][] = $strtag;
                    }	
                }
            }
            $highLighReviews[] = $row;
        }

        $pageVars['reviews'] = $highLighReviews;

        // prepare page bar of search result
        $currentURL = htmlspecialchars( $_SERVER["REQUEST_URI"] );
        $currentURL = preg_replace( '/[&\?]p=[0-9]+/', "", $currentURL );
        $pageObj = new Pager( $currentURL, $reviewCount, REVIEW_PER_PAGE, $page, UP_RESULT_PAGE );

        $pageVars['pagebar'] = $pageObj->getBar();
    }
}
else{
     //normal database query
    $reviews = $reviewdao->GetByProductId( $pid, REVIEW_PER_PAGE, $page );
    $reviewCount = $reviewdao->GetReviewCountByShopId( $pid );

    if( $reviews ) {
        $pageVars['reviews'] = array();

        foreach( $reviews as $row ) {
            $row['content'] = sconv( EdpSubStr( $row['content'], 0, 200 ) );
            $row['tags'] = sconv( $row['tags'] );
            $row['tags_plain'] = $row['tags'];

            $pageVars['reviews'][] = $row;
        }

        // prepare page bar of search result
        $currentURL = htmlspecialchars( $_SERVER["REQUEST_URI"] );
        $currentURL = preg_replace( '/[&\?]p=[0-9]+/', "", $currentURL );
        $pageObj = new Pager( $currentURL, $reviewCount, REVIEW_PER_PAGE, $page, UP_RESULT_PAGE );

        $pageVars['pagebar'] = $pageObj->getBar();
    }

}
?>
